package com.platform.server.util;
import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.SMS;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;

/**
 * 极光推送
 * @author Administrator
 *
 */
public class AuroraPush {

	/*public static String secret = "54916f48cb9202ee182f2563";
	public static String appkey = "693ddebc4f9ec500b94aed0b";*/
	
	public static String secret = "3c9cfc4761e8bb3565419c70";
	public static String appkey = "60d701be71730a1cef633489";
	
	public static String secret1 = "9607898ef70af630abfdfc57";
	public static String appkey1 = "df47f52c82ec9cb8b15f7b7e";
	
	/*public static String secret = "de73bd5c8eb89b04ab165728";
	public static String appkey = "099a69c4e23a36a63ff7145a";*/
	
	public static void pushAll(String msg){
		JPushClient jpushClient = new JPushClient(secret, appkey, null, ClientConfig.getInstance());

	    // For push, all you need do is to build PushPayload object.
	    PushPayload payload = buildPushObject_all_all_alert(msg);

	    try {
	        PushResult result = jpushClient.sendPush(payload);
	        System.out.println("pushAll="+result);
//	        LOG.info("Got result - " + result);
	    } catch (APIConnectionException e) {
	    	e.printStackTrace();
//	        LOG.error("Connection error, should retry later", e);
	    } catch (APIRequestException e) {
	        // Should review the error, and fix the request
	        //LOG.error("Should review the error, and fix the request", e);
	        //LOG.info("HTTP Status: " + e.getStatus());
	        //LOG.info("Error Code: " + e.getErrorCode());
	        //LOG.info("Error Message: " + e.getErrorMessage());
	    	e.printStackTrace();
	    }
	}
	
	//别名推、个推
	public static void pushTargetForDriver(String where,String msg){
		JPushClient jpushClient = new JPushClient(secret, appkey, null, ClientConfig.getInstance());
       System.out.println(msg);
	    // For push, all you need do is to build PushPayload object.
	    PushPayload payload = buildPushObject_all_alias_alert(where,msg);

	    try {
	        PushResult result = jpushClient.sendPush(payload);
	        System.out.println(result);
	        //LOG.info("Got result - " + result);
	    } catch (APIConnectionException e) {
	    	e.printStackTrace();
	    	// Connection error, should retry later
	        //LOG.error("Connection error, should retry later", e);
	    } catch (APIRequestException e) {
	    	e.printStackTrace();
	    	// Should review the error, and fix the request
	        //LOG.error("Should review the error, and fix the request", e);
	        //LOG.info("HTTP Status: " + e.getStatus());
	        //LOG.info("Error Code: " + e.getErrorCode());
	        //LOG.info("Error Message: " + e.getErrorMessage());
	    }
	}
	
	public static void pushTargetForGuide(String where,String msg){
		JPushClient jpushClient = new JPushClient(secret1, appkey1, null, ClientConfig.getInstance());
       System.out.println(msg);
	    // For push, all you need do is to build PushPayload object.
	    PushPayload payload = buildPushObject_all_alias_alert(where,msg);

	    try {
	        PushResult result = jpushClient.sendPush(payload);
	        System.out.println(result);
	        //LOG.info("Got result - " + result);
	    } catch (APIConnectionException e) {
	    	e.printStackTrace();
	    	// Connection error, should retry later
	        //LOG.error("Connection error, should retry later", e);
	    } catch (APIRequestException e) {
	    	e.printStackTrace();
	    	// Should review the error, and fix the request
	        //LOG.error("Should review the error, and fix the request", e);
	        //LOG.info("HTTP Status: " + e.getStatus());
	        //LOG.info("Error Code: " + e.getErrorCode());
	        //LOG.info("Error Message: " + e.getErrorMessage());
	    }
	}
	 //消息
	public static void pushTargetMsg(String title,String msgContent,String alias,String aurora_alias){
		JPushClient jpushClient = new JPushClient(secret1, appkey1, null, ClientConfig.getInstance());
	    // For push, all you need do is to build PushPayload object.

	    try {
	    	System.out.println("title"+title);
	    	System.out.println("msgContent"+msgContent);
	    	System.out.println("alias"+alias);
	    	if("ios".equals(aurora_alias)){
	    		PushResult result1 = jpushClient.sendIosMessageWithAlias(title, msgContent, alias);
		        System.out.println("ios消息="+result1);
	    	}else{
	    		PushResult result = jpushClient.sendAndroidMessageWithAlias(title, msgContent, alias);
		        System.out.println("Android消息="+result);
	    	}
	    } catch (APIConnectionException e) {
	    	e.printStackTrace();
	    	// Connection error, should retry later
	        //LOG.error("Connection error, should retry later", e);
	    } catch (APIRequestException e) {
	    	e.printStackTrace();
	    	// Should review the error, and fix the request
	        //LOG.error("Should review the error, and fix the request", e);
	        //LOG.info("HTTP Status: " + e.getStatus());
	        //LOG.info("Error Code: " + e.getErrorCode());
	        //LOG.info("Error Message: " + e.getErrorMessage());
	    }
	}
	//快捷地构建推送对象：所有平台，所有设备，内容为 ALERT 的通知
	public static PushPayload buildPushObject_all_all_alert(String msg) {
        return PushPayload.alertAll(msg);
    }
	//所有平台，推送目标是别名为 "alias1"，通知内容为 ALERT
	public static PushPayload buildPushObject_all_alias_alert(String where,String msg) {
		 return PushPayload.newBuilder()
	     	.setPlatform(Platform.all())
	        .setAudience(Audience.alias(where))
	        .setNotification(Notification.alert(msg))
	        .build();
	}
	//平台是 Android，目标是 tag 为 "tag1" 的设备，内容是 Android 通知 ALERT，并且标题为 TITLE
	/*public static PushPayload buildPushObject_android_tag_alertWithTitle() {
		return PushPayload.newBuilder()
	    	.setPlatform(Platform.android())
	        .setAudience(Audience.tag("tag1"))
	        .setNotification(Notification.android(ALERT, TITLE, null))
	        .build();
	}*/
	//平台是 iOS，推送目标是 "tag1", "tag_all" 的交集，推送内容同时包括通知与消息 
	//	- 通知信息是 ALERT，角标数字为 5，通知声音为 "happy"，并且附加字段 from = "JPush"；
	//	消息内容是 MSG_CONTENT。通知是 APNs 推送通道的，消息是 JPush 应用内消息通道的。
	//	APNs 的推送环境是“生产”（如果不显式设置的话，Library 会默认指定为开发）
/*	public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage() {
		return PushPayload.newBuilder()
				.setPlatform(Platform.ios())
				.setAudience(Audience.tag_and("tag1", "tag_all"))
				.setNotification(Notification.newBuilder()
						.addPlatformNotification(IosNotification.newBuilder()
								.setAlert(ALERT)
								.setBadge(5)
								.setSound("happy")
						.addExtra("from", "JPush")
						.build())
	        	.build())
				.setMessage(Message.content(MSG_CONTENT))
				.setOptions(Options.newBuilder()
						.setApnsProduction(true)
						.build())
				.build();
	}*/
	//平台是 Andorid 与 iOS，推送目标是 （"tag1" 与 "tag2" 的并集）交（"alias1" 与 "alias2" 的并集），
	//	推送内容是 - 内容为 MSG_CONTENT 的消息，并且附加字段 from = JPush
/*	public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras() {
		return PushPayload.newBuilder()
				.setPlatform(Platform.android_ios())
				.setAudience(Audience.newBuilder()
						.addAudienceTarget(AudienceTarget.tag("tag1", "tag2"))
	                    .addAudienceTarget(AudienceTarget.alias("alias1", "alias2"))
	                    .build())
	            .setMessage(Message.newBuilder()
	                    .setMsgContent(MSG_CONTENT)
	                    .addExtra("from", "JPush")
	                    .build())
	            .build();
	}*/
	
	//推送内容包含SMS信息
	public static void testSendWithSMS() {
		JPushClient jpushClient = new JPushClient(secret, appkey);
	    try {
	    	SMS sms = SMS.content("Test SMS", 10);
	        PushResult result = jpushClient.sendAndroidMessageWithAlias("Test SMS", "test sms", sms, "alias1");
	        //LOG.info("Got result - " + result);
	    } catch (APIConnectionException e) {
	        //LOG.error("Connection error. Should retry later. ", e);
	    } catch (APIRequestException e) {
	        //LOG.error("Error response from JPush server. Should review and fix it. ", e);
	        //LOG.info("HTTP Status: " + e.getStatus());
	        //LOG.info("Error Code: " + e.getErrorCode());
	        //LOG.info("Error Message: " + e.getErrorMessage());
	    }
	}
	/*public static PushPayload buildPushObject_all_all_alert(String ALERT) {
        return PushPayload.alertAll(ALERT);
    }*/
	public static void main(String[] args) {
		
//		buildPushObject_all_alias_alert("16","测试mq");
//		buildPushObject_all_all_alert("测试mq");
	//	pushTarget("16","899999999");
	}
}
